package com.tim.app.ui.fragment.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.application.library.base.BaseFragment;
import com.application.library.runtime.event.EventListener;
import com.application.library.runtime.event.EventManager;
import com.tim.app.R;
import com.tim.app.constant.EventTag;
import com.tim.app.ui.activity.MainActivity;
import com.tim.app.ui.fragment.selceted.GoodAtTopicFragment;
import com.tim.app.ui.fragment.selceted.QuestionAndAnswerFragment;
import com.tim.app.ui.fragment.selceted.SelectedFragment;

/**
 * 问答
 */
public class HomePageFragment extends BaseFragment {

    public static final String TAG = "HomePageFragment";
    private View rootView;

    private int currentViewIndex;//当前页面的索引

    static final int INDEX_SELECTED = 0;//精选
    static final int INDEX_ANSWER = 1;//问答
    static final int INDEX_SUBJECT = 2;//话题

    SelectedFragment selectedFragment;
    QuestionAndAnswerFragment questionAndAnswerFragment;
    GoodAtTopicFragment subjectFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_tab_homepage, container, false);
        }
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventManager.ins().registListener(EventTag.GOTO_ANSWER, eventListener);
        EventManager.ins().registListener(EventTag.GOTO_SELECTED, eventListener);
        EventManager.ins().registListener(EventTag.GOTO_SUBJECT, eventListener);
        EventManager.ins().registListener(EventTag.GO_BACK, eventListener);
    }

    EventListener eventListener = new EventListener() {
        @Override
        public void handleMessage(int what, int arg1, int arg2, Object dataobj) {
            switch (what) {
                case EventTag.GOTO_ANSWER:
                    showAnswerFragment();
                    break;
                case EventTag.GOTO_SELECTED:
                    showSelectedFragment();
                    break;
                case EventTag.GOTO_SUBJECT:
                    showSubjectFragment();
                    break;
                case EventTag.GO_BACK:
                    int index = (int) dataobj;
                    if (MainActivity.TAB_INDEX_SELECTED == index) {
                        if (INDEX_SUBJECT == currentViewIndex) {
                            showAnswerFragment();
                        } else if (INDEX_ANSWER == currentViewIndex) {
                            showSelectedFragment();
                        }
                    }
                    break;
            }
        }
    };

    /**
     * 显示精选页面
     */
    public void showSelectedFragment() {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (null == selectedFragment) {
            selectedFragment = new SelectedFragment();
        }
        transaction.replace(R.id.flContainer, selectedFragment);
        transaction.commitAllowingStateLoss();
        currentViewIndex = INDEX_SELECTED;
        ((MainActivity) getActivity()).setInFirstPage(true);
    }


    /**
     * 显示问答页面
     */

    public void showAnswerFragment() {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (null == questionAndAnswerFragment) {
            questionAndAnswerFragment = new QuestionAndAnswerFragment();
        }
        questionAndAnswerFragment.initData();
        transaction.replace(R.id.flContainer, questionAndAnswerFragment);

        transaction.commitAllowingStateLoss();
        currentViewIndex = INDEX_ANSWER;
        ((MainActivity) getActivity()).setInFirstPage(false);
    }

    /**
     * 显示话题页面
     */
    public void showSubjectFragment() {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        if (null == subjectFragment) {
            subjectFragment = new GoodAtTopicFragment();
        }
        subjectFragment.initData();
        transaction.replace(R.id.flContainer, subjectFragment);
        transaction.commitAllowingStateLoss();
        currentViewIndex = INDEX_SUBJECT;
        ((MainActivity) getActivity()).setInFirstPage(false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
        switch (currentViewIndex) {
            case INDEX_SELECTED:
                showSelectedFragment();
                break;
            case INDEX_ANSWER:
                showAnswerFragment();
                break;
            case INDEX_SUBJECT:
                showSubjectFragment();
                break;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView&& null != rootView.getParent()) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventManager.ins().removeListener(EventTag.GOTO_ANSWER, eventListener);
        EventManager.ins().removeListener(EventTag.GOTO_SELECTED, eventListener);
        EventManager.ins().removeListener(EventTag.GOTO_SUBJECT, eventListener);
        EventManager.ins().removeListener(EventTag.GO_BACK, eventListener);
    }


}
